<?php
namespace frontend\controllers;

use frontend\models\ArtForm;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\base\ErrorException;
use yii\helpers\Html;
use common\widgets\file_upload\FileUpload;
use common\models\member\Member;
use common\models\category\Category;
use common\models\articles\Articles;
use yii\data\Pagination;

class DynamicsController extends Controller
{     
	// public $layout = 'ucenter';	
	public function actions()
    {
        return [
            'upload'=>[
                'class' => 'common\widgets\file_upload\UploadAction',     //这里扩展地址别写错
                'config' => [
                ]
            ],
			'ueditor'=>[
				'class' => 'common\widgets\ueditor\UeditorAction',
				'config'=>[
					//上传图片配置
					'imageUrlPrefix' => "", /* 图片访问路径前缀 */
					'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
				]
			]
        ];
    }

    /**
    * 新闻列表
    */
   	public function actionNews(){
	 	$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = $this->action->id;
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}

	 	$query = Articles::find()->where(['category_id'=>$categoryid]);
		$count = $query->count();
		$page = new Pagination(['totalCount' => $count, 'pageSize' => '30']);
		$articles = $query->offset($page->offset)
		    ->limit($page->limit)
		    ->all();
		    //var_dump($articles);


        $childcontent = Articles::find()->where(['status' => 1, 'category_id'=>$categoryid])->asArray()->all();

		$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();
        return $this->render('news',[
            'child' => $child,
            'childcontent' => $articles,
            'page' => $page,
        ]);
   	}

    /**
    * 新闻内页
    */
   	public function actionNewspage($tid){
   		$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = 'news';
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}
	 	$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();
		/**
		* 文章上一篇和下一篇
		*/
		$query = Articles::find()->where(['category_id'=>$categoryid])->asArray()->all();
		var_dump($query);

   		$article = Articles::find()->where(['id' => $tid])->asArray()->one();
   		//$lastone =
   		//$lastone = 
   		return $this->render('newspage',[
   			'article' => $article,
   			'child' => $child,
   			//'lastone' => $lastone,
   			//'nextone' => $nextone,
        ]);
   	}

    /**
    * 新闻列表
    */
   	public function actionNotice(){
	 	$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = $this->action->id;
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}

	 	$query = Articles::find()->where(['category_id'=>$categoryid]);
		$count = $query->count();
		$page = new Pagination(['totalCount' => $count, 'pageSize' => '30']);
		$articles = $query->offset($page->offset)
		    ->limit($page->limit)
		    ->all();
		    //var_dump($articles);


        $childcontent = Articles::find()->where(['status' => 1, 'category_id'=>$categoryid])->asArray()->all();

		$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();
        return $this->render('news',[
            'child' => $child,
            'childcontent' => $articles,
            'page' => $page,
        ]);
   	}

   	public function actionNoticepage($tid){
   		$category = Category::find()->where(['status'=> 1])->asArray()->all();
	 	foreach($category as $t){
	 		if(!empty($t['url'])){
		 		$arr = explode("/", $t['url']);
		 		$action = 'notice';
		 		if($arr[1] == $action){
		 			$categoryid = $t['id'];
		 		}
		 	}
	 	}
	 	$data = Category::find()->where(['id' => $categoryid])->asArray()->one();
		$parent_id = $data['parent_id'];
		$child = Category::find()->where(['parent_id' => $parent_id])->asArray()->all();


   		$article = Articles::find()->where(['id' => $tid])->asArray()->one();
   		return $this->render('newspage',[
   			'article' => $article,
   			'child' => $child,
        ]);
   	}
}